<?php
class MShop extends MY_Model {

	const STATE_DISABLE = 0;
	const STATE_ENABLE = 1;
	const STATE_DELETE = 2;
    const IS_FORBIDDEN = 1;
    const NOT_FORBIDDEN = 0;
	protected $table = 'shops';
	private $filter = array();

    function __construct() {
        parent::__construct();
    }

    public function getAllShops(){
        $this->db->select( $this->table.'.*');
        $this->db->distinct();
        $this->db->from($this->table);
        $this->db->where('status',1);
        if($this->_user->role_id == 2){
            $this->db->where('group_id',$this->_user->group_id);
        }
        if ($this->_user->role_id == 4){
            $this->db->where('owner_id',$this->_user->id);
        }

        $query = $this->db->get();
        $queryResult =  $query->result();
        $kvArr = array();
        foreach ($queryResult as $k=>$v){
            $kvArr[$v->id] = $v->name;
        }
        return $kvArr;
    }

    public function getAllShopsByAmazon(){
        $this->db->select( $this->table.'.*');
        $this->db->distinct();
        $this->db->from($this->table);
        $this->db->where('status',1);
        if (in_array($this->_user->role_id,[2,4])){
            $this->db->where('group_id',$this->_user->group_id);
        }

        $query = $this->db->get();
        return $query->result();
    }

    public function getShopsByGroupId($group_id){
        $this->db->select( $this->table.'.*');
        $this->db->distinct();
        $this->db->from($this->table);
        $this->db->where('group_id',$group_id);
        $this->db->where('status',1);
        $query = $this->db->get();
        $queryResult =  $query->result();
        $kvArr = array();
        foreach ($queryResult as $k=>$v){
            $kvArr[$v->id] = $v->name;
        }
        return $kvArr;
    }

    public function getAllShopsTrue(){
        $this->db->select( $this->table.'.*');
        $this->db->distinct();
        $this->db->from($this->table);
        $query = $this->db->get();
        $queryResult =  $query->result();
        $kvArr = array();
        foreach ($queryResult as $k=>$v){
            $kvArr[$v->id] = $v->name;
        }
        return $kvArr;
    }

    public function getAllShop(){
        $this->db->select( $this->table.'.*');
        $this->db->distinct();
        $this->db->from($this->table);
        if(in_array($this->_user->role_id,[2,4])){
            $this->db->where('group_id',$this->_user->group_id);
        }

        $query = $this->db->get();
        $queryResult =  $query->result();
        $kvArr = array();
        foreach ($queryResult as $k=>$v){
            $kvArr[$v->id] = $v->name;
        }
        return $kvArr;
    }

    public static function getMState($state = NULL){
        $f = Array(self::NOT_FORBIDDEN=>'启用', self::IS_FORBIDDEN=>'禁用');
        if($state === NULL){
            return $f;
        }
        return element($state, $f, '');
    }

    public static function getState($state=NULL){
    	$define = array(self::STATE_DISABLE=>'未启用',self::STATE_ENABLE=>'正常',self::STATE_DELETE=>'已注销');
    	if($state === NULL){
    		return $define;
    	}else{
    		return key_exists($state, $define)?$define[$state]:'';
    	}
    }

	public function getList($limit, $sort=false){
		$this->db->select( $this->table.'.*');
		$this->db->distinct();
		$this->db->from($this->table);
		$this->db->limit($limit['limit'],$limit['offset']);
		$this->formfilter->doFilter();
		if($this->_user->role_id != 1){
		    $this->db->where('group_id',$this->_user->group_id);
		}
		if($sort){
			$this->db->order_by('sort', 'asc');
		}else{
			$this->db->order_by('id', 'desc');
		}
		$query = $this->db->get();
		return $query->result();
	}
	public function getCount(){
		$this->db->select('count(distinct g.id) as num');
		$this->db->from($this->table . ' as g');
		$this->formfilter->doFilter();
		if($this->_user->role_id != 1){
		  $this->db->where('group_id',$this->_user->group_id);
		}
		$tmp = $this->db->get()->result();
		return (int)$tmp[0]->num;
	}

	public function getById($id){
		$query = $this->db->where('id',$id)->get($this->table);
		return $query->row();
	}

	public function getMember($limit,$sort=false){
        $this->db->select( 's.group_id,a.*,p.name as p_name');
        $this->db->distinct();
        $this->db->from($this->table.' s');
        $this->db->join('core_admins a','a.group_id = s.group_id','LEFT');
        $this->db->join('core_admin_group p','s.group_id = p.id','LEFT');
        $this->db->limit($limit['limit'],$limit['offset']);
        $this->formfilter->doFilter();
        if($this->_user->role_id != 1){
            $this->db->where('a.group_id',$this->_user->group_id);
        }
        if($sort){
            $this->db->order_by('sort', 'asc');
        }else{
            $this->db->order_by('id', 'desc');
        }
        $query = $this->db->get();
        return $query->result();
    }

    public function getMemberCount(){
        $this->db->select('count(distinct g.id) as num');
        $this->db->from($this->table . ' as g');
        $this->db->join('core_admins a','a.group_id = g.group_id','LEFT');
        $this->formfilter->doFilter();
        if($this->_user->role_id != 1){
            $this->db->where('a.group_id',$this->_user->group_id);
        }
        $tmp = $this->db->get()->result();
        return (int)$tmp[0]->num;
    }
}
